package com.cs.beneficios.web.action;

import com.cs.beneficios.validator.Validaciones;
import com.cs.paoyer.administration.business.model.UserVO;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;
import com.cs.beneficios.common.ConnectionFactory;

public class SubModSubprogramaAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String strvalor1 = null, strvalor2 = null, strvalor3 = null, strvalor4 = null, strvalor5 = null, strvalor6 = null, strvalor7 = null;
		String strvalor8 = null, strvalor9 = null, strvalor10 = null, strvalor11 = null, strvalor12 = null, strvalor13 = null, strvalor14 = null, strvalor15 = null, strvalor16 = null;
		String strSql = null;
		Connection conn = null;
		boolean existe = false;
		String strOption = "modsub";
		request.setAttribute("option", strOption);
		// ConnectionFactory cf = new
		// ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
		// database db = new database();
		Validaciones validacion = new Validaciones(cf);
		HttpSession session = request.getSession();

		try {
			conn = cf.getConnection();
			// conn = db.getConnectionWithDriver("beneficios","beneficios");
		} catch (SQLException e) {
		}
		String subprog = request.getParameter("Subprograma");
		Object obsub = request.getParameter("Subprograma");
		if (obsub != null) {
			subprog = request.getParameter("Subprograma");
		} else {
			subprog = request.getParameter("Codigo");
		}

		try {
			strvalor1 = request.getParameter("subprograma");
		} catch (Exception e) {
			strvalor1 = "";
		}
		try {
			strvalor14 = request.getParameter("codearea");
		} catch (Exception e) {
			strvalor14 = "";
		}
		try {
			strvalor2 = request.getParameter("descripcion");
		} catch (Exception e) {
			strvalor2 = "";
		}
		try {
			strvalor16 = request.getParameter("code");
		} catch (Exception e) {
			strvalor16 = "";
		}
		try {
			strvalor3 = request.getParameter("diasutil");
		} catch (Exception e) {
			strvalor3 = "";
		}
		try {
			strvalor4 = request.getParameter("valor");
		} catch (Exception e) {
			strvalor4 = "";
		}
		try {
			strvalor5 = request.getParameter("desde");
		} catch (Exception e) {
			strvalor5 = "";
		}
		try {
			strvalor6 = request.getParameter("hasta");
		} catch (Exception e) {
			strvalor6 = "";
		}
		try {
			strvalor7 = request.getParameter("grupo");
		} catch (Exception e) {
			strvalor7 = "";
		}
		try {
			strvalor8 = request.getParameter("estudiantes");
		} catch (Exception e) {
			strvalor8 = "";
		}
		try {
			strvalor9 = request.getParameter("sisben");
		} catch (Exception e) {
			strvalor9 = "";
		}
		try {
			strvalor10 = request.getParameter("vigencia");
			if (strvalor10.equals("I")) {
				session.setAttribute("Codigo", subprog);
				return mapping.findForward("success2");
			}
		} catch (Exception e) {
			strvalor10 = "";
		}

		try {
			strvalor11 = request.getParameter("admon");
		} catch (Exception e) {
			strvalor11 = "";
		}
		try {
			strvalor12 = request.getParameter("admincodigo");
		} catch (Exception e) {
			strvalor12 = "";
		}
		try {
			strvalor15 = request.getParameter("diasrenovar");
		} catch (Exception e) {
			strvalor15 = "";
		}
		// Proceso para la auditoria
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		// Fin del proceso
		if (strvalor12.equalsIgnoreCase("")) {

			strSql = " update tcs_ben_subprograma set  usu_codigo = '"
					+ strvalor11
					+ "', nombre = UPPER('"
					+ strvalor1
					+ "'), "
					+ " prg_codigo = "
					+ strvalor16
					+ ", descripcion = UPPER('"
					+ strvalor2
					+ "'), dias_reutilizar = '"
					+ strvalor3
					+ "',  grupo_familiar= '"
					+ strvalor7
					+ "', "
					+ " validar_contra_estud = '"
					+ strvalor8
					+ "', validar_contra_sisben = '"
					+ strvalor9
					+ "', "
					+ " valor_unitario_beneficio = '"
					+ strvalor4
					+ "', desde_vigencia = to_date('"
					+ strvalor5
					+ "','dd/mm/rrrr'), "
					+ " hasta_vigencia = TO_DATE('"
					+ strvalor6
					+ "','dd/mm/rrrr'), vigencia = '"
					+ strvalor10
					+ "', area_codigo = '"
					+ strvalor14
					+ "', CAMPO_A3 = '"
					+ strvalor15
					+ "', "
					+ " USUARIO_MODIFICA = ?, FECHA_MODIFICA = to_date(?,'dd/mm/rrrr'), "
					+ " TERMINAL_MODIFICA = ? " + " WHERE codigo= ? ";
			// System.out.println(strSql);
		} else {

			strSql = " update tcs_ben_subprograma set  usu_codigo = '"
					+ strvalor12
					+ "', nombre = UPPER('"
					+ strvalor1
					+ "'), "
					+ " prg_codigo = "
					+ strvalor16
					+ ", descripcion = UPPER('"
					+ strvalor2
					+ "'), dias_reutilizar = '"
					+ strvalor3
					+ "',  grupo_familiar= '"
					+ strvalor7
					+ "', "
					+ " validar_contra_estud = '"
					+ strvalor8
					+ "', validar_contra_sisben = '"
					+ strvalor9
					+ "', "
					+ " valor_unitario_beneficio = '"
					+ strvalor4
					+ "', desde_vigencia = TO_DATE('"
					+ strvalor5
					+ "','dd/mm/rrrr'), "
					+ " hasta_vigencia = TO_DATE('"
					+ strvalor6
					+ "','dd/mm/rrrr'), vigencia = '"
					+ strvalor10
					+ "', area_codigo = '"
					+ strvalor14
					+ "' , CAMPO_A3 = '"
					+ strvalor15
					+ "', "
					+ " USUARIO_MODIFICA = ?, FECHA_MODIFICA = to_date(?,'dd/mm/rrrr'), "
					+ " TERMINAL_MODIFICA = ? " + " WHERE codigo= ? ";
		}
		String strPar[] = { user.getName(), dateFormat.format(c.getTime()),
				strTerminal, subprog };

		try {
			int modificar = cf.execSql(strSql, strPar, conn);

			if (modificar > 0) {
				session.setAttribute("mensajes",
						"El Beneficio fue modificado satisfactoriamente...");
			}
		} catch (SQLException e) {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSUB - cwsub ********/");
			session.setAttribute("mensajes",
					"El beneficio no pudo ser modificado");
		} finally {
			try {
				cf.closeConnection(conn);
			} catch (SQLException e) {
				System.err.println("/******** TDSUB - cwsub");
				e.printStackTrace();
				System.err.println("TDSUB - cwsub ********/");
			}

		}
		return mapping.findForward("success");
	}
}